package com.cyandpos.pad.offline.model;

import android.database.Cursor;

import com.cyandpos.pad.http.OkHttpUtils;
import com.cyandpos.pad.offline.offlinedb.CursorUtil;
import com.cyandpos.pad.offline.offlinedb.Sqldb;
import com.cyandpos.pad.posdata.Goods;
import com.cyandpos.pad.utils.ComSharedPreferences;

import java.math.BigDecimal;
import java.util.Map;

public class OfflineQueryGoods {

    public static void QueryGood(Sqldb sqldb, Map<String, Object> params, OkHttpUtils.ResultCallback callback){
        int SP_ID=-1000;
        int DEPTID=0;
        Goods goods = new Goods();
        try {
            Cursor cursor1 = sqldb.find("select BARCODE,SP_ID from SPBARCODE where BARCODE='"+params.get("barcode")+"'");
            if (cursor1 != null && cursor1.getCount() > 0) {
                while (cursor1.moveToNext()) {
                    SP_ID = CursorUtil.getInt(cursor1, "SP_ID");
                }
                    Cursor cursor2 = sqldb.find("select SP_ID,SPCODE,BARCODE,NAME,UNIT,SPGG,PACKED,szspbj,FKCXSBJ,SPTYPE,HSFS,LSDJ,HYLSDJ,STATUS,SPFL,SHOPID,SLYXS,FKCXSSJ from SPXX  where SP_ID=" + SP_ID + " and FDBH=" +"'"+ params.get("storeCode")+"'");
                    if (cursor2 != null && cursor2.getCount() > 0) {
                        while (cursor2.moveToNext()) {
                            goods.setInputCode(CursorUtil.getString(cursor2, "SPCODE"));
                            goods.setSaleCount(CursorUtil.getInt(cursor2,"SLYXS")+"");
                            if ("0.0".equals((CursorUtil.getDouble(cursor2, "LSDJ")) + "")) {
                                goods.setPrice("0");
                            } else {
                                goods.setPrice(CursorUtil.getDouble(cursor2, "LSDJ") + "");
                            }
                            goods.setUnit(CursorUtil.getString(cursor2, "UNIT"));
                            goods.setName(CursorUtil.getString(cursor2, "NAME"));
                            goods.setGoodsGuid(CursorUtil.getMyUUID());
                            goods.setId(SP_ID + "");
                            goods.setCode(CursorUtil.getString(cursor2, "SPCODE"));
                            goods.setBarCode(CursorUtil.getString(cursor2, "BARCODE"));
                            goods.setBulkType(CursorUtil.getInt(cursor2, "SPTYPE") + "");
                            goods.setCanSaleCount(CursorUtil.getInt(cursor2, "FKCXSSJ") + "");
                            goods.setClassType(CursorUtil.getString(cursor2, "SPTYPE"));
                            goods.setDiscount("0.0");
                            goods.setHasedRefundCount("0");
                            goods.setImageUrl("");
                            goods.setInx("0");
                            goods.setMaxSaleCount(CursorUtil.getInt(cursor2, "FKCXSSJ") + "");
                            goods.setPackaged(false);//PACKED
                            goods.setPreferentialMoney("0.0");
                            goods.setRefundCount("");
                            goods.setSaleMoney((new BigDecimal(CursorUtil.getDouble(cursor2, "LSDJ")).multiply(new BigDecimal(CursorUtil.getInt(cursor2, "SLYXS")))) + "");
                            goods.setTicktGoodsInx(((int) ComSharedPreferences.getValueByName("INX", "goodsInx", 1, "") + 1) + "");
                            ComSharedPreferences.WriteSharedPreferences("INX","goodsInx",Integer.parseInt(goods.getTicktGoodsInx()));//脱机商品序号
                            goods.setTicktInx("0");
                            Cursor cursor3 = sqldb.find("select W.DEPTID,B.BMDM from GTSP W,BM B where W.DEPTID=B.DEPTID and W.SP_ID =" + SP_ID);
                            if (cursor3 != null && cursor3.getCount() > 0) {
                                while (cursor3.moveToNext()) {
                                    DEPTID = CursorUtil.getInt(cursor3, "DEPTID");
                                    goods.setDeptId(DEPTID + "");
                                    goods.setDeptCode(CursorUtil.getString(cursor3, "BMDM"));
                                }
                            }
                        }
                        cursor2.close();
                        callback.onSuccess(CursorUtil.getOfflineBean(goods));
                    } else {
                        callback.onFailure("没有此商品！");
                        return;
                    }
                cursor1.close();
            } else {
                callback.onFailure("没有此商品！");
                return;
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
